apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service"], function(V, $api, $, app, service) {
        var vm = {
            el: '.container',
            data: {
                showList: [],
                idx: 0
            },
            mounted: function() {
                var that = this;
                api.addEventListener({
                    name: 'finishGoodsAdd'
                }, function(ret, err) {
                    if (ret) {
                        that.init();
                    }
                });

                this.init();
            },
            methods: {
                init: function() {
                    var that = this;
                    var idx = api.pageParam.index;
                    this.idx = idx;

                    var shopId = api.pageParam.shopId;
                    var unionId = $api.getStorage("shop_unionid");
                    // 获取商品列表
                    app.ajax(
                        service['searchGoods'], {
                            data: {
                                values: {
                                    shop_code: shopId,
                                    unionid: unionId
                                }
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                if (res.data.length == 0) {
                                    api.toast({
                                        msg: '您还没有商品，请添加商品',
                                        duration: 2000,
                                        location: 'middle'
                                    });
                                } else {
                                    var goodsList = res.data;
                                    that.showList = goodsList;
                                }
                            } else {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        }
                    );
                },

                // 前往添加商品
                toAddGoods: function() {
                    var shopId = api.pageParam.shopId;
                    app.openWin('shopAdd', {
                        shopId: shopId
                    });
                },

                // 保存关联信息
                saveMessage: function() {
                    var goodsId = $("input[name='radio']:checked").val();
                    if (goodsId == '' || goodsId == null) {
                        api.toast({
                            msg: '您还没选中需要关联的商品',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    var goodsName = $("input[name='radio']:checked").attr('data-name');
                    var cover = $("input[name='radio']:checked").attr('data-cover');
                    var price = $("input[name='radio']:checked").attr('data-price');
                    var item = this.idx;
                    var obj = {
                        index: item,
                        goodsId: goodsId,
                        goodsName: goodsName,
                        cover: cover,
                        price: price
                    }

                    api.sendEvent({
                        name: 'goodsId',
                        extra: obj
                    });

                    api.sendEvent({
                        name: 'getGoodsId',
                        extra: obj
                    });

                    api.closeWin({
                        name: 'shopCard_Link'
                    });
                }
            }
        };
        var even = function() {}
        var newVue = new Vue(vm);
        even();

    })
}
